package com.mikelouis.test.scala.proginscala.chapter23

/**
 * Created by yuazhu on 2/2/2015.
 */
object First {
  def main(args: Array[String]) {
    case class Person(name: String, isMale: Boolean, children: Person*)
    val lara = Person("Lara", false)
    val bob = Person("Bob", true)
    val julie = Person("Julie", false, lara, bob)
    val persons = List(lara, bob, julie)

    // find the pair of maman and her children
    println(persons filter (p => !p.isMale) flatMap (p =>
      (p.children map (c => (p.name, c.name)))))

    // equivalent implementation
    // but scala compiler will convert this to the first expression
    // more concretly, yield method will be converted to the combination of map, flatMap, and filter
    // non yield method will be converted to the invocation of filter and foreach
    println(for (p <- persons; if !p.isMale; c <- p.children)
    yield (p.name, c.name))
  }

}
